//
//  XDJCViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/24.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "XDJCViewController.h"

#import "XDJCCell.h"
#import "XDDetViewController.h"
@interface XDJCViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *array;
}

@property (nonatomic,strong)UITableView *tableView;

@end

@implementation XDJCViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [self showNav:@"信贷教程"];
    array = [NSMutableArray array];
    [self loadUI];
    [self loadData];
    
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
}
#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    XDJCCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"XDJCCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.labBank.layer.cornerRadius = 5.0;
    cell.labBank.layer.borderColor = COLOR_RGB(65, 132, 255, 1).CGColor;
    cell.labBank.layer.borderWidth = 0.5;
    NSDictionary *dic = array[indexPath.row];
    
    cell.labBank.text = [dic objectForKey:@"name"];
    cell.labTit.text = [dic objectForKey:@"credit_title"];
    cell.labDet.text = [dic objectForKey:@"subject"];
    NSString *date = [dic objectForKey:@"add_time"];
    cell.labDate.text = [self getNewDate:date];
    
    NSString *watch = [NSString stringWithFormat:@" %li",[[dic objectForKey:@"watch"] integerValue]];
    NSString *pay = [NSString stringWithFormat:@" %li",[[dic objectForKey:@"paycount"] integerValue]];
    [cell.btnWatch setTitle:watch forState:UIControlStateNormal];
    [cell.btnPay setTitle:pay forState:UIControlStateNormal];
    
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    XDDetViewController *det = [[XDDetViewController alloc] init];
    NSDictionary *dic = array[indexPath.row];
    det.bank = [dic objectForKey:@"name"];
    det.tit = [dic objectForKey:@"credit_title"];
    NSString *date = [dic objectForKey:@"add_time"];
    NSString *xdid = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"id"] integerValue]];
    
    det.date = [self getNewDate:date];
    det.watch = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"watch"] integerValue]];
    det.pay = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"paycount"] integerValue]];
    det.xdid = xdid;
    [self.navigationController pushViewController:det animated:YES];
    
    [self loadData2:xdid];
    
}
//查看
- (void)loadData2:(NSString *)xdid{
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:xdid forKey:@"id"];
    NSString *url = [NSString stringWithFormat:@"%@/App/Credit/addwatch",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
}

//信贷-列表
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:@"1" forKey:@"p"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = [NSString stringWithFormat:@"%@/App/Credit/index",urlHead];
    [manager GET:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        array = [responseObject objectForKey:@"result"];
        
        [_tableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
